﻿#region License
// Copyright (c) 2012 Mund Consulting Ltd
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

namespace SharePoint.Foundation.Publishing.WebParts.SearchBox
{
    [ToolboxItemAttribute(false)]
    public class SearchBox : WebPart, IWebEditable
    {
        /// <summary>
        /// This webpart renders search text box with scope functionality
        /// </summary>
        #region Constants
        private const string constBtnImageUrl = "/SpfStyleLibrary/mc/mcimages/srch_btn.png";
        private const string constSiteColUrl = "~sitecollection/";
        string scope;
        string scopeOption;
        

        #endregion

        #region Members

        string setImageUrl = constBtnImageUrl;

        public string SetUrl { get; set; }

        public string Scope
        {
            get
            {
                return scope;
            }
            set
            {
                scope = value;
            }
        }

        public string ScopeOption
        {
            get
            {
                if (scopeOption == constSiteColUrl)
                {
                    return SPContext.Current.Site.Url;
                }
                return scopeOption;
            }
            set
            {
                scopeOption = value;
            }
        }

        public string SetImageUrl
        {
            get
            {
                return setImageUrl;
            }
            set
            {
                setImageUrl = value;
            }
        }
        TextBox tbxSearch;
        ImageButton imgBtnSearch;

        #endregion

        #region Event Handlers

        protected override void CreateChildControls()
        {
            tbxSearch = new TextBox();
            tbxSearch.Width = 180;
            tbxSearch.Attributes["class"] = "ms-searchbox";
            this.Controls.Add(tbxSearch);

            imgBtnSearch = new ImageButton();
            imgBtnSearch.Width = 32;
            imgBtnSearch.Height = 32;
            imgBtnSearch.ImageUrl = SetImageUrl;
            this.Controls.Add(imgBtnSearch);

            imgBtnSearch.Click += new ImageClickEventHandler(Btn_Click);
        }

        protected void Btn_Click(object sender, EventArgs e)
        {
            
            if ( string.IsNullOrEmpty(SetUrl))
            {
                string currentUrl = HttpContext.Current.Request.Url.AbsolutePath.ToString();
                HttpContext.Current.Response.Redirect(currentUrl + "?k=" + tbxSearch.Text + "&u=" + HttpUtility.UrlEncode(ScopeOption));
            }
            else
            {

                HttpContext.Current.Response.Redirect(SetUrl + "?k=" + tbxSearch.Text + "&u=" + HttpUtility.UrlEncode(ScopeOption));
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //To maintain the value of search text box and dropdown throughout the page redirection.
            if (HttpContext.Current.Request.QueryString["k"] != null)
            {
                tbxSearch.Text = HttpContext.Current.Request.QueryString["k"].ToString();
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["u"]))
                {
                    Scope = HttpUtility.UrlEncode(HttpContext.Current.Request.QueryString["u"]).ToString();
                }
            }

            writer.Write("<table><tr><td width=\"130px\">");
            tbxSearch.RenderControl(writer);
            writer.Write("</td>");
            writer.Write("<td>");
            imgBtnSearch.RenderControl(writer);
            imgBtnSearch.Attributes.Add("style","float:left");
            writer.Write("</td>");
            writer.Write("</td></tr></table>");
        }
        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            // control the editorparts
            List<EditorPart> editors = new List<EditorPart>();
            SearchBoxEditorPart editorPart = new SearchBoxEditorPart();
            editorPart.ID = this.ID + "_editorPart";
            editors.Add(editorPart);
            return new EditorPartCollection(editors);

        }
        #endregion
    }
}
